<?php
namespace Admin\Controller;
use Think\Controller;
class GoodsCarTypeController extends PublicController {
    public function index(){
        $this->display();
    }
    
    //商品展示
    public function get_all_goods_list(){
       $limit = I('get.limit');
       $page = I('get.page');
       $like = I('get.like');
      if(!empty($like)){
          $where['pinyin|name']=array("like","%".$like."%");
       }
        $ret['total'] = M('goods_car_type')->count();
        $ret['data'] = M('goods_car_type')->alias('a')
        ->field('a.id,a.logo_name,a.car_name,a.car_img,car_pirce')
        ->join('__CAR_TYPE__ as b on a.car_type_id=b.id')
        ->where($where)
        ->page($page,$limit)
        ->select();
        foreach ($ret['data'] as $key => $value) {
            $ret['data'][$key]['car_img'] = '<img style="width:50px;" src="'.$ret['data'][$key]['car_img'].'">';
           }
        
        $ret['code'] = 200;
        ret($ret);
    }

     //单图上传
    public function car_ico(){
      $name = date("Ymd_His_",time()).rand(100000,999999);
      copy($_FILES['file']['tmp_name'],'Public/upload/goodscartype/car_img/'.$name.'.jpg');
      $ret['isSuccess'] = 1;
      $ret['msg'] = "上传成功";
      $ret['src'] = '/Public/upload/goodscartype/car_img/'.$name.'.jpg';
      ret($ret);
    }

    //多图上传
    public function uploadImage(){
      //ret($_FILES);exit();
      $name = date("Ymd_His_",time()).rand(100000,999999);
          copy($_FILES['file']['tmp_name'],'Public/upload/goodscartype/car_imgs/'.$name.'.jpg');
      $ret['isSuccess'] = 1;
      $ret['msg'] = '上传成功！';
      $ret['data']['src'] = '/Public/upload/goodscartype/car_imgs/'.$name.'.jpg';
      ret($ret);
    }
     //轮播上传
    public function broadcastImage(){
      //ret($_FILES);exit();
      $name = date("Ymd_His_",time()).rand(100000,999999);
          copy($_FILES['file']['tmp_name'],'Public/upload/goodscartype/car_broadcast/'.$name.'.jpg');
      $ret['isSuccess'] = 1;
      $ret['msg'] = '上传成功！';
      $ret['data']['src'] = '/Public/upload/goodscartype/car_broadcast/'.$name.'.jpg';
      ret($ret);
    }
 
    //商品添加跳转页面
    public function add(){
      $shop=M('shop')->select();
      $typename=M('car_type')->field('id,name')->select();
      $this->typename = $typename; //品牌数组
      $this->shop = $shop;//车型数组
      $this->display();
    }
 
    //商品添加
    public function insert(){
      $data['typename']=$_POST['typename'];
      $data['car_type_id']=$_POST['cartypeid'];
      $data['logo_name']=$_POST['logoname'];
      $data['car_img']=$_POST['logo'];
      $data['car_name']=$_POST['carname'];
      $data['car_pirce']=$_POST['carpirce'];
      $data['car_broadcast']=json_encode($_POST['broadcast']);
      $data['car_baozhi']=$_POST['carbaozhi'];
      $data['car_ranking']=$_POST['carranking'];
      $data['car_html']=$_POST['html'];   
      $data['car_imgs']=json_encode($_POST['images']);    
      $data['tagging']=$_POST['tagging'];
      $data['shop_id']=json_encode($_POST['shop_id']);     
      $re=M('goods_car_type')->add($data);
      if($re) {
        $ret['isSuccess']=1;
        $ret['msg']="添加成功";
        ret($ret);
      }
      else{
        $ret['isSuccess']=1;
        $ret['msg']="添加失败";
        ret($ret);
      } 
    }
    //商品修改
    public function edit(){
      $data['id']=I('get.id'); //接受修改商品ID
      $this->data = M('goods_car_type')->where(array('id'=>$data['id']))->find();
      $shop=M('shop')->select();
      $shop_id=json_decode($this->data['shop_id']);
      foreach ($shop_id as $key => $value) {          //循环goods_car_type表里的shop_id
         foreach ($shop as $ke => $val) {             // 在循环shop表里的所有数据，拿每一条跟上层的数据对比
          //var_dump($shop[$ke]['id']);
            if($shop_id[$key]==$shop[$ke]['id']){
                $shop[$ke]['checked']=1;
            }
         }
      }
   
      $broadcast=json_decode($this->data['car_broadcast']);
      $broadcasts = array();
      foreach ($broadcast as $key => $value) {
        $iii['broadcast_src'] = $broadcast[$key];
        $iii['broadcast_resrc'] = str_replace('/', '',$broadcast[$key]);
        $iii['broadcast_resrc'] = str_replace('.', '',$iii['broadcast_resrc']);
        $iii['broadcast_resrc'] = str_replace('_', '',$iii['broadcast_resrc']);
        array_push($broadcasts,$iii); //组成新数组保存地址路径跟修改后的地址路径，用于前台图片展示与删除
      }
      $imgs=json_decode($this->data['car_imgs']);
      $images=array();
      foreach ($imgs as $ke => $val) {
        $ig['car_imgs']=$imgs[$ke];
        $ig['car_reimgs']=str_replace('/','',$imgs[$ke]);
        $ig['car_reimgs']=str_replace('.','',$ig['car_reimgs']);
        $ig['car_reimgs']=str_replace('_','',$ig['car_reimgs']);
        array_push($images,$ig);
      }
      $typename=M('car_type')->field('id,name')->select();
      foreach ($typename as $k => $va) {
        if($typename[$k]['id']==$this->data['car_type_id']){
          $typename[$k]['checked']=1;
        }else{
          $typename[$k]['checked']=0;
        }
      }

      $this->broadcasts = $broadcasts; //轮播图数组
      $this->images = $images; //相册数组
      $this->typename = $typename; //品牌数组
      $this->shop = $shop;//车型数组
      $this->display();
    }
    //商品修改提交
    public function update(){
      $data['id']=$_POST['id'];
      $data['car_type_id']=$_POST['cartypeid'];
      $data['logo_name']=$_POST['logoname'];
      $data['car_name']=$_POST['carname'];
      $data['car_pirce']=$_POST['carpirce'];
      $data['car_baozhi']=$_POST['carbaozhi'];
      $data['car_ranking']=$_POST['carranking'];
      $data['tagging']=$_POST['tagging'];
      $data['shop_id']=json_encode($_POST['shop_id']);

      $data['car_img']=$_POST['logo'];
      $data['car_broadcast']=json_encode($_POST['broadcast']);
      $data['car_imgs']=json_encode($_POST['images']);
      $data['car_html']=$_POST['html'];  
      $re=M('goods_car_type')->where(array('id'=>$data['id']))->save($data);
      if($re!==false){
        $ret['isSuccess']=1;
        $ret['msg']="修改成功";
        ret($ret);
      }else{
        $ret['isSuccess']=0;
        $ret['msg']="修改失败";
        ret($ret);
      }

    }

    //删除商品
    public function del_goods(){
      $data['id']=I('post.id');
      $re=M('goods_car_type')->where('id='.$data['id'])->delete();
      if($re){
        $ret['isSuccess']=1;
        $ret['msg']="删除成功";
        ret($ret);
      }else{
        $ret['isSuccess']=0;
        $ret['msg']="删除失败";
        ret($ret); 
      }
    }

    //测试
    public function ceshi(){
       $data['good_id']=1;
       $data['specs_name']=I('post.spec_name');
       $data['specs_price']=I('post.spec_price');
       foreach ($data['specs_name'] as $key => $value) {
        $data['name']=$data['specs_name'][$key];
        $data['price']=$data['specs_price'][$key];
        M('goods_specs')->add($data);
       }
    }

      public function test(){
        $data['code']=1;
        $data['msg']='OK';
        echo $this->ajaxReturn($data);
      }

}
